package com.wg.photo.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.wg.photo.ABSOnItemClickListener;
import com.wg.photo.R;
import com.wg.photo.utils.DirImage;
import com.wg.photo.utils.GroupImage;
import com.wg.photo.utils.PickPreferences;

import java.util.List;

/**
 * Created by Went_Gone on 2017/5/16.
 */

public class PickListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private GroupImage groupImage;
    private DirImage dirImage;
    private Context context;
    private ABSOnItemClickListener absOnItemClickListener;

    public void setAbsOnItemClickListener(ABSOnItemClickListener absOnItemClickListener) {
        this.absOnItemClickListener = absOnItemClickListener;
    }

    public PickListAdapter(Context c, View.OnClickListener listener) {
        this.context = c;
        this.groupImage = PickPreferences.getInstance(context).getListImage();
        this.dirImage = PickPreferences.getInstance(context).getDirImage();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final GroupImageViewHolder holder = new GroupImageViewHolder(LayoutInflater.from(context).inflate(R.layout.pick_item_list_layout, parent, false));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (absOnItemClickListener!=null){
                    absOnItemClickListener.onItemClick(v,holder.getAdapterPosition());
                }
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(dirImage != null) {
            String dirName = dirImage.dirName.get(position);
            List<String> paths = groupImage.mGroupMap.get(dirName);
            GroupImageViewHolder groupImageViewHolder = (GroupImageViewHolder) holder;
            groupImageViewHolder.bindItem(dirName, paths);
        }

    }

    @Override
    public int getItemCount() {
        if(dirImage != null ) {
            return dirImage.dirName.size();
        }else {
            return 0;
        }
    }

    private class GroupImageViewHolder extends RecyclerView.ViewHolder{

        private ImageView cover,open;
        private TextView dirNameText,photoSizeText;

        GroupImageViewHolder(View itemView) {
            super(itemView);
            cover = (ImageView) itemView.findViewById(R.id.iv_cover);
            dirNameText = (TextView) itemView.findViewById(R.id.tv_dir_name);
            photoSizeText = (TextView) itemView.findViewById(R.id.tv_photo_size);
        }

        void bindItem(String dirName, List<String> paths){
            dirNameText.setText(dirName);
//            photoSizeText.setText(String.format(context.getString(R.string.pick_photo_size),paths.size() + ""));
            photoSizeText.setText(String.format(paths.size() + ""));
            Glide.with(context).load(Uri.parse("file://" + paths.get(0))).thumbnail(0.3f).into(cover);
            itemView.setTag(R.id.pick_dir_name,dirName);
        }

    }
}